<?php

	$lp_root_path = './';
	$general_root_path = '../';

	define('INSIDE', true);
	include($general_root_path . 'extension.inc');
	include($general_root_path . 'common.'.$phpEx);

include_once($lp_root_path . 'classes/templates.class.'.$phpEx);
include_once($lp_root_path . 'procedures.class.'.$phpEx);
include_once($lp_root_path . 'classes/json_data_classes.'.$phpEx);
$dentro=check_user();

$nick = $_SESSION['nick'];
$pass = $_SESSION['pass'];

$result = new json_request();

/*
ERROR LIST:

-1		->	No session
-2		->	No request data, missing "jsondata" parameter
-3		->	Type not implemented
-4		->	Operation not implemented
-5		->	Exception

*/

if(!$dentro)
{
	$result->error=1;
	$result->error_number=-1;
}
else
{
	if($jsonraw==null)
	{
		$result->error=1;
		$result->error_number=-2;
	}
	else
	{
		$jsonraw = str_replace('\"','"',$jsonraw);
		$request = new json_request();
		$request->load(json_decode($jsonraw));

		
		$sp = new procedures();

		try
		{
			//type "0" is reserver por special operation types
			switch($request->type)
			{
				case 1: //User operation
					switch($request->operation)
					{
						case 0:		//Ask for user light data
							$row = $sp->sp_users_getUserSimple($nick,$pass);
							
							$userdata = new json_userData();
							$userdata->nick = $row[0]["nick"];
							$userdata->maxBuild = $row[0]["maxBuild"];
							$userdata->maxResearch = $row[0]["maxResearch"];
							$userdata->maxFleet = $row[0]["maxFleet"];
							$userdata->totalPlanets = $row[0]["totalPlanets"];
							$userdata->maxPlanets = $row[0]["maxPlanets"];
							
							$result->error=0;
							$result->error_number=0;
							$result->data=$userdata;
							
							break;
						default:	//Operation not implemented
							$result->error=1;
							$result->error_number=-4;
							break;	
					}
					break;
				default: //Type not implemented
					$result->error=1;
					$result->error_number=-3;
					
					break;
			}
		}
		catch (exception $e)
		{
			die($e->getMessage());
			$result->error=1;
			$result->error_number=-5;
			$result->data=$e->getMessage();
		}
	}
}

echo json_encode($result);

?>
